<?php

use app\configure\model\ConfigHouseModel;
use Endroid\QrCode\Exceptions\DataDoesntExistsException;
use Endroid\QrCode\Exceptions\ImageFunctionFailedException;
use Endroid\QrCode\Exceptions\ImageFunctionUnknownException;
use Endroid\QrCode\Exceptions\ImageTypeInvalidException;
use Endroid\QrCode\QrCode;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 获取性别
 * @param $sex
 * @return string
 */
function getSex($sex): string
{
    switch ($sex) {
        case 1:
            $label = '男';
            break;
        case 2:
            $label = '女';
            break;
        default:
            $label = '未知';
            break;
    }
    return $label;
}

/**
 * 获取业主类别
 * @param $type
 * @return string
 */
function getUserType($type): string
{
    switch ($type) {
        case 0:
            $label = '业主';
            break;
        case 2:
            $label = '租户';
            break;
        case 1:
            $label = '家人';
            break;
        default:
            $label = '访客';
            break;
    }
    return $label;
}

/**
 * 获取巡查类型
 * @param $type
 * @return string
 */
function getPatrolType($type): string
{
    switch ($type) {
        case 0:
            $label = '日';
            break;
        case 2:
            $label = '月';
            break;
        case 1:
            $label = '周';
            break;
        case 3:
            $label = '年';
            break;
    }
    return $label;
}

/**
 * 汽车类型
 * @param $type
 * @return string
 */
function getCarType($type): string
{
    switch ($type) {
        case 1:
            $label = '中型车';
            break;
        case 2:
            $label = '大型车';
            break;
        default:
            $label = '小型车';
            break;
    }
    return $label;
}

/**
 * @param $status
 * @return string
 */
function getShareStatus($status): string
{
    switch ($status) {
        case 1:
            $label = '进行中';
            break;
        case 2:
            $label = '待支付';
            break;
        case -1:
            $label = '已取消';
            break;
        default:
            $label = '待审核';
            break;
    }
    return $label;
}

/**
 * 字符串截取
 * @param $text
 * @param $limit
 * @return string
 */
function getSubstr($text, $limit): string
{
    return mb_substr($text, 0, $limit);
}


function array2string($str): string
{
    $arr = json_decode($str, true);
    if (empty($arr)) return '';
    return implode(',', $arr);
}


/**
 * 轮播图类型
 * @param $type
 * @return string
 */
function getBannerType($type): string
{
    switch ($type) {
        case 0 :
            $label = '首页';
            break;
        case 1:
            $label = '邻享';
            break;
        case 2:
            $label = '商城';
            break;
        case 3:
            $label = '龙庭社圈';
            break;
        case 4:
            $label = '无忧社区';
            break;
        case 5:
            $label = '员工端首页';
            break;
        default:
            $label = '无效banner';
            break;
    }

    return $label;
}


/**
 * 商户类型
 * @param $type
 * @return string
 */
function getShopType($type): string
{
    switch ($type) {
        case 1:
            $label = '会员';
            break;
        case 2:
            $label = '旗舰店';
            break;
        case 0:
        default:
            $label = '普通';
            break;
    }

    return $label;
}


/**
 * 权限管理类型
 * @param $type
 * @return string
 */
function getStaffType1($type): string
{
    switch ($type) {
        case 1:
            $label = '个人';
            break;
        case 0:
        default:
            $label = '首页';
            break;
    }

    return $label;
}

function getOrderType($type): string
{
    switch ($type) {
        case 1:
            $label = '物业费';
            break;
        case 2:
            $label = '车位费';
            break;
        case 3:
            $label = '报事报修';
            break;
        case 4:
            $label = '活动';
            break;
        case 5:
            $label = '邻里共享';
            break;
        case 7:
            $label = '物业直销';
            break;
        default:
            $label = '商城';
            break;
    }

    return $label;
}

function getOrderStatus($status): string
{
    switch ($status) {
        case 1:
            $label = '预订单';
            break;
        case 2:
            $label = '待支付';
            break;
        case 3:
            $label = '支付中';
            break;
        case 4:
            $label = '已支付';
            break;
        case 5:
            $label = '已发货';
            break;
        case 6:
            $label = '已完成';
            break;
        default:
            $label = '已取消';
            break;
    }

    return $label;
}

function getQuestionType($type): string
{
    switch ($type) {
        case 1:
            $label = '单选';
            break;
        case 2:
            $label = '多选';
            break;
        case 3:
            $label = '文本';
            break;
        default:
            $label = '自定义';
            break;
    }

    return $label;
}

/**
 * 图片截取拿第一个
 * @param $string
 * @return mixed|string
 */
function picJsonDecode($string)
{
    $images = json_decode($string, true);
    return $images[0] ?? '';
}

/**
 * 时间截取
 * @param $time
 * @return false|string
 */
function timeSub($time)
{
    if ($time == 0) return '永久';

    return date("Y-m-d", $time);
}

/**
 * 时间格式化
 * @param $time
 * @return false|string
 */
function timeChange($time)
{
    if (!$time || $time == '') return '';
    return date("Y-m-d H:i:s");
}

/**
 * 获取房产信息名称
 * @param $id
 * @return string
 * @throws DataNotFoundException
 * @throws DbException
 * @throws ModelNotFoundException
 */
function getFloor($id): string
{
    $model = new \app\configure\model\ConfigHouseModel();

    $floor = $model->where(['id' => $id])->find();
    if (!$floor) return '未选择';

    return $floor->name;
}

/**
 * 新闻类型
 * @param $type
 * @return string
 * @throws DataNotFoundException
 * @throws DbException
 * @throws ModelNotFoundException
 */
function getNewsType($type): string
{
    $model = new \app\configure\model\ConfigNewsTypeModel();
    $type = $model->where(['id' => $type])->find();
    if (!$type) return '未选择';

    return $type->name;
}

/**
 * 数据脱敏
 * @param $string 需要脱敏值
 * @param int $length 结束
 * @param string $re 脱敏替代符号
 * @return bool|string
 * 例子:
 * dataDesensitization('18811113683', 3, 4); //188****3683
 * dataDesensitization('乐杨俊', 0, -1); //**俊
 */
function dataDesensitization($string, $length = 0, $re = '*')
{
    $length >= 6 ? $start = 6 : $start = 3;
    if (empty($string)) {
        return false;
    }
    $strarr = array();
    $mb_strlen = mb_strlen($string);
    while ($mb_strlen) {//循环把字符串变为数组
        $strarr[] = mb_substr($string, 0, 1, 'utf8');
        $string = mb_substr($string, 1, $mb_strlen, 'utf8');
        $mb_strlen = mb_strlen($string);
    }
    $strlen = count($strarr);
    $begin = $start >= 0 ? $start : ($strlen - abs($start));
    $end = $last = $strlen - 1;
    if ($length > 0) {
        $end = $begin + $length - 1;
    } elseif ($length < 0) {
        $end -= abs($length);
    }
    for ($i = $begin; $i <= $end; $i++) {
        $strarr[$i] = $re;
    }
    if ($begin >= $end || $begin >= $last || $end > $last) return false;
    return implode('', $strarr);
}


/**
 * 获取维修类型
 * @param $type
 * @return string
 * @throws DataNotFoundException
 * @throws DbException
 * @throws ModelNotFoundException
 */
function getRepair($type): string
{
    $model = new \app\configure\model\ConfigRepairModel();

    $repair = $model->where(['id' => $type])->find();
    if (!$repair) return '未选择';

    return $repair->name;
}

/**
 * @param $length
 * @return false|string
 */
function shufflerCode($length)
{
    $permitted_chars = '0123456789#abcdefghilkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    return substr(str_shuffle($permitted_chars), 0, $length);
}

/**
 * @param $code
 * @param $name
 * @return string
 * @throws ImageTypeInvalidException
 */
function patrolQrCode($code, $name): string
{
    $qrCode = new QrCode();
    $qrCode
        ->setText($code)
        ->setSize(300)
        ->setPadding(10)
        ->setErrorCorrection('high')
        ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
        ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
        ->setLabel($name)
        ->setLabelFontSize(16)
        ->setImageType(QrCode::IMAGE_TYPE_PNG);

    return $qrCode->getDataUri();
}

/**
 * 获取车位信息
 * @param $id
 * @return string
 * @throws DataNotFoundException
 * @throws DbException
 * @throws ModelNotFoundException
 */
function getPark($id): string
{
    $model = new \app\configure\model\ConfigParkModel();

    $park = $model->where(['id' => $id])->find();
    if (!$park) return '未选择';

    return $park->name;
}

/**
 * 邻享服务
 * @param $id
 * @return string
 * @throws DataNotFoundException
 * @throws DbException
 * @throws ModelNotFoundException
 */
function getShareItem($id): string
{
    $model = new \app\configure\model\ConfigShareItemModel();
    $item = $model->where(['id' => $id])->find();
    if (!$item) return '已关闭服务';

    return $item->name;
}

/**
 * 获取话题id
 * @param $id
 * @return string
 * @throws DataNotFoundException
 * @throws DbException
 * @throws ModelNotFoundException
 */
function getLatticeTopic($id): string
{
    $model = new \app\configure\model\ConfigLatticeTopicModel();
    $topic = $model->where(['id' => $id])->find();
    if (!$topic) return '';

    return '# ' . $topic->name . ' #';
}

/**
 * 获取话题id
 * @param $id
 * @return string
 * @throws DataNotFoundException
 * @throws DbException
 * @throws ModelNotFoundException
 */
function getTopic($id): string
{
    $model = new \app\configure\model\ConfigCommunityTopicModel();
    $topic = $model->where(['id' => $id])->find();
    if (!$topic) return '';

    return '# ' . $topic->name . ' #';
}

/**
 * 获取房屋信息
 * @param $village
 * @param $unit
 * @param $floor
 * @param $house
 * @return string
 * @throws DataNotFoundException
 * @throws DbException
 * @throws ModelNotFoundException
 */
function getHouse($village, $unit, $floor, $house): string
{
    $model = new ConfigHouseModel();
    $village1 = $model->where(['id' => $village])->find();;
    if (!$village1) {
        return '-';
    }
    $unit1 = $model->where(['id' => $unit])->find();
    if (!$unit1) {
        return '-';
    }
    $floor1 = $model->where(['id' => $unit])->find();
    if (!$floor1) {
        return '-';
    }
    $house1 = $model->where(['id' => $house])->find();
    if (!$house1) {
        return '-';
    }

    return $village1['name'] . $unit1['name'] . $floor1['name'] . $house1['name'];
}

/**
 * 获取员工类型
 * @param $type
 * @return string
 * @throws DataNotFoundException
 * @throws DbException
 * @throws ModelNotFoundException
 */
function getStaffType($type): string
{
    $model = new \app\staff\model\StaffTypeModel();
    $staff = $model->where(['id' => $type])->find();
    if (!$staff) return '';
    return $staff->name;
}

